package com.cqut.forum.service.imp;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.cqut.forum.dao.UserNewsDao;
import com.cqut.forum.entity.UserNews;
import com.cqut.forum.service.ModuleService;
import com.cqut.forum.service.UserNewsService;

@Component
public class UserNewsServiceImp implements UserNewsService {
	@Resource
	UserNewsDao userNewsDao;
	@Resource
	ModuleService moduleService;
	
	/*@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> getMessages(int userId, int index, int size) {
		int moduleIndex = moduleService.getModuleIndex("留言");
		List<Map<String, Object>> messages = userNewsDao.getMessages(userId,moduleIndex,index,size);
		List<Map<String, Object>> messageReplys = new ArrayList<Map<String,Object>>();
		if(messages.size()>0){
			List<Integer> messagesId = new ArrayList<Integer>();
			for(Map<String, Object> m:messages){
				messagesId.add((Integer)m.get("user_news_id"));
			}
			messageReplys = userNewsDao.getMessageReply(messagesId);
		}
		
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> map:messages){
			Map<String,Object> m = new HashMap<String, Object>();
			m.put("userId", map.get("user_id"));
			m.put("image", map.get("user_photo"));
			m.put("name", map.get("user_name"));
			m.put("content", map.get("news_contents"));
			m.put("time", ((Date)map.get("news_time")).toLocaleString());
			//如果if判断为false，则该map存储内容为article，否则为picture
			m.put("articleId", map.get("user_news_id"));
			List<Map<String, Object>> reply = new ArrayList<Map<String,Object>>();
			for(Map<String, Object> r:messageReplys){
				if(map.get("user_news_id").equals(r.get("user_news_id"))){
					Map<String, Object> rep = new HashMap<String, Object>();
					rep.put("userId", r.get("user_id"));
					rep.put("image", r.get("user_photo"));
					rep.put("name", r.get("user_name"));
					rep.put("content", r.get("news_contents"));
					rep.put("time", ((Date)r.get("news_time")).toLocaleString());
					reply.add(rep);
				}
			}
			m.put("reply", reply);
			
			result.add(m);
		}
		return result;
	}*/

	@Override
	public UserNews addUserNews(UserNews news) {
		UserNews un = userNewsDao.save(news);
		return un;
	}

	@Override
	public UserNews isVisited(int visitor, int userId) {
		UserNews un = userNewsDao.isVisited(visitor, userId);
		return un;
	}

/*	@Override
	public boolean haveReadMyMessages(int userId, String moduleName) {
		int moduleId = moduleService.getModuleIndex(moduleName);
		boolean result = userNewsDao.haveReadMyMessages(userId,moduleId);
		return result;
	}*/

}
